#! /usr/bin/python
#coding=utf-8 
#----PYTHON FOR SIGNATRUE TESTING TOOL----
import sqlite3
import httplib
import socket
import telnetlib
import sys
import time
import re
import locale


def connect_sql_db(dbname):
	print("connect sqlite db name:"+dbname)
	db=sqlite3.connect(dbname)
	return db

def write_file(f,msg,flag,log):
	if(flag == True):
		m = "signature_id:" +str(msg) +"  Pass\n"
	else:
		m = "signature_id:" +str(msg) +"  Faile\n" +str(log)+"\n"
	f.seek(0,2)
	f.write("**********************\n")
	f.write(m)
	return

	
def	db_check(db,f1,f2,sig):
	cur =db.cursor()
	if(sig != None):
		cur.execute('select * from signature_rule where signature_id = \'%s\''%(str(sig)))
	else:
		cur.execute('select * from signature_rule ')
	for row in cur:
		try:
			ro = str(row[2])
		except UnicodeEncodeError as elog:
			ro = row[2].encode('utf-8')
		if(ro.find("GET",0,3) < 0):
			#this package is POST or RESPONSE packet
			col = ro.split('\r\n')
			count = len(col)
			found = 0
			for i in col:
				if(i.find('Content-Length:') >=0):
					found = 1
					break;

			if(found):
				if i[15:].find("<#") >= 0:
					#msg ="---------signature %s  content-length error!\n%s\n"%(row[9],i)
					msg ="---------signature %s  content-length error!\n%s\n"%(row[9],i)
					f2.seek(0,2)
					f2.write(msg)
					continue
				else:
					try:
						ro_count = locale.atoi(i[15:])
					except:
						msg ="-------signature %s content-lenth may have problem\n"%((row[9]))
						f2.seek(0,2)
						f2.write(msg)
						ro_count = locale.atoi(i[16:])
				byte = ro.find('\r\n\r\n',0,len(ro)- 4) + 4
				if(byte > 0):
					count = len(ro[byte:])
					j = 0
					while ro.find('<#',byte,len(ro)) >= 0:
						j = ro.find('<#',byte,len(ro))
						count = count - 4
						byte = j+2
					new = 'Content-Length: %d'%(count)
					if ro_count != count :
						msg = "Sig %s change content-lenth %d to %d \n"%(row[9],ro_count,count)
						f2.seek(0,2)
						f2.write(msg)
					else:
						msg = "Sig %s content lenth is correct!\n"%(row[9])
						f1.seek(0,2)
						f1.write(msg)
						continue
				else:
					msg = "Sig %s NOt find content-lenth\n"%(row[9])
					f2.seek(0,2)
					f2.write(msg)
					continue
			else:
				msg = "Sig %s not found content-lenth\n"%(row[9])
				f2.seek(0,2)
				f2.write(msg)
				continue
		else:
			#packet is GET
			#print("GET MESAGE!")
			if ro.find('\r\n\r\n') >= 0:
				if len(ro.split('\r\n')) != len(ro.split('\n')):
					msg = "sig %s rn not eq n\n"%(row[9])
					f2.seek(0,2)
					f2.write(msg)
					continue
				line = ro.split('\r\n')
				for i in line:
					if i.find(' ',0,1) >= 0:
						msg = "sig %s not have rnrn tailline  %s!\n"%(row[9],i)
						f2.seek(0,2)
						f2.write(msg)

			else:
				msg = "sig %s not have rnrn tail !\n"%(row[9])
				f2.seek(0,2)
				f2.write(msg)

	cur.close()
	return	

		



def main(dbname):
	#连接sqlite数据库,登陆fortiweb后台
	db = connect_sql_db(dbname)
	
	f1 = open("content_count.txt","w")
	f2 = open("packet_format.txt","w")
	if(len(sys.argv) == 3):
		db_check(db,f1,f2,sys.argv[2])
	else:
		db_check(db,f1,f2,None)
	f1.close()
	f2.close()
	db.close()
	sys.exit()





if len(sys.argv) < 2:
	usage()
	sys.exit()

Sql_db = str(sys.argv[1])
Back_file = str("result.txt")

if __name__ == "__main__":
	main(Sql_db)


